package com.example.it.looker.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;


/**
 * Created by 鲁迅认识的那只猹 on 9/3/2017 10:45 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */


/**
 * BaseViewPagerAdapter提供了循环滚动的实现
 */
public abstract class BaseLoopViewPageAdapter<T> extends PagerAdapter {
    /**
     * 上下文对象
     */
    private Context context;
    /**
     * 数据源
     */
    private List<T> dataSource;
    /**
     * 资源ID
     */
    private int resId;

    public BaseLoopViewPageAdapter(Context context, List<T> dataSource, int resId) {
        this.context = context;
        this.dataSource = dataSource;
        this.resId = resId;
        //初始化数据
        init();
    }


    /**
     * 初始化数据
     */
    private void init() {
        T t1 = dataSource.get(0);
        T t2 = dataSource.get(this.dataSource.size() - 1);
        dataSource.add(t1);
        dataSource.add(0, t2);
    }

    /**
     * 返回多连个的数量，以达到无线循环的效果
     */
    @Override
    public int getCount() {
        return this.dataSource.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /**
     * 实例化显示的对象
     */
    @Override
    public abstract Object instantiateItem(ViewGroup container, int position);


    /**
     * 回收资源
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    /**
     * 获取指定的资源
     */
    public int getResId() {
        return resId;
    }

    /**
     * 获取上下文菜单
     */
    public Context getContext() {
        return context;
    }

    /**
     * 获取数据
     */
    public T getItemData(int position) {
        return dataSource.get(position);
    }
}
